<template lang="pug">
.case
  .case-header
    .case-header-search
      el-input(placeholder="案例名称/发布者" prefix-icon="el-icon-search" v-model="searchString" @change="changeSearch" clearable)
    .case-header-info 设置案例为经典案例，最多设置7个案例
  .case-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
      :cell-style="cellStyle"
    )
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column(label="案例名称" prop="projectName" min-width="100")
        template(slot-scope="scope")
          router-link.table-item-bd(:to="{ name: 'caseDetail', params: { id: scope.row.materialCaseId } }")
            .table-item-case-name {{scope.row.projectName}}
      el-table-column(label="发布者" prop="designOwner")
      el-table-column(label="案例地址" prop="location")
      el-table-column(label="创建时间" sortable prop="caseDate")
      el-table-column(label="浏览量" sortable align="right" prop="viewCount")
      el-table-column(label="操作", v-if="$permission('AB-01004-000')")
        template(slot-scope="scope")
          .table-item-handle
            el-switch(
              v-model="scope.row.isClassic"
              active-color="#8676FF"
              inactive-color="#B2B4C7"
              :disabled="scope.row.isDisable"
              @change="changeClassCase(scope.row)"
            )
    .table-pagination
      el-pagination(
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        @current-change="currentChange"
      )
</template>

<script>
import { getCaseList, setClassicCase } from 'api'
export default {
  name: 'caseList',
  data () {
    return {
      loading: false,
      total: 0,
      page: 1,
      pageSize: 20,
      searchString: '',
      list: []
    }
  },
  computed: {
    maxNum () {
      let len = this.list.filter(item => item.isClassic).length
      if (len >= 7) {
        this.list.forEach(item => {
          if (!item.isClassic) item.isDisable = true
          else item.isDisable = false
        })
      } else {
        this.list.forEach(item => {
          item.isDisable = false
        })
      }
      return len
    }
  },
  methods: {
    async changeClassCase (item) {
      try {
        if (this.maxNum > 7) {
          this.$message.error('经典案例最多设置7个！')
          return
        }
        await this.$axios.post(setClassicCase, { materialCaseId: item.materialCaseId, isClassic: item.isClassic })
        this.$message.success('设置成功！')
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    cellStyle ({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0 && row.isClassic) {
        return { background: '#FFF5F8' }
      }
    },
    currentChange (page) {
      this.page = page
      this.getList()
    },
    changeSearch () {
      this.page = 1
      this.getList()
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query, currentPage: this.page, pageLines: this.pageSize }
        if (this.searchString) query.searchString = this.searchString
        const { ResultSet } = await this.$axios.post(getCaseList, { ...query })
        let len = ResultSet.userData.materialCaseList.filter(item => item.isClassic).length
        if (len >= 7) {
          ResultSet.userData.materialCaseList.forEach(item => {
            if (!item.isClassic) item.isDisable = true
            else item.isDisable = false
          })
        }
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.materialCaseList
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.getList()
  }
} 
</script>

<style lang="scss" scoped>
.case {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.case-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
}
.case-table {
  height: calc(100% - 150px);
}
.case-header-info {
  font-size: 14px;
  line-height: 22px;
  color: #8B8EAB;
}
.table-item-case-name {
  color: #FF2E42;
}
.table-item-header {
  display: flex;
  align-items: center;
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
</style>
